package com.q.tqb.activity.mine.wallet;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import com.q.tqb.R;
import com.q.tqb.activity.mine.setting.SettingBindTelActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.model.BindCardInfo;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.MD5Util;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WalletAddCardActivity extends BaseActivity {


    @BindView(R.id.et_id_name)
    EditText mEtIdName;
    @BindView(R.id.ll_id_name)
    LinearLayout mLlIdName;
    @BindView(R.id.et_id_no)
    EditText mEtIdNo;
    @BindView(R.id.ll_id_no)
    LinearLayout mLlIdNo;
    @BindView(R.id.et_card_name)
    TextView mEtCardName;
    @BindView(R.id.ll_card_name)
    LinearLayout mLlCardName;
    @BindView(R.id.et_card_no)
    EditText mEtCardNo;
    @BindView(R.id.ll_card_no)
    LinearLayout mLlCardNo;
    @BindView(R.id.et_tel_no)
    EditText mEtTelNo;
    @BindView(R.id.ll_tel_no)
    LinearLayout mLlTelNo;
    @BindView(R.id.tv_add)
    TextView mTvAdd;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    private int uid;
    private BindCardInfo mBindCardInfo;
    private MyCountDownTimer myCountDownTimer;


    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_add_card;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("添加银行卡");
        uid = (int) SPUtil.get(this, "uid", 0);
        myCountDownTimer = new MyCountDownTimer(1000 * 120, 1000);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_add, R.id.et_card_name, R.id.tv_get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add:
                final AlertDialog.Builder normalDialog = new AlertDialog.Builder(WalletAddCardActivity.this);
                normalDialog.setTitle("温馨提示");
                normalDialog.setMessage("请确认银行卡信息，错误信息将无法提现到银行卡");
                normalDialog.setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                addCard();
                            }
                        });
                normalDialog.setNegativeButton("再去检查", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                normalDialog.show();
                break;
            case R.id.et_card_name:
                showBankDialog();
                break;
            case R.id.tv_get_code:
                getCode();
                break;
            default:
                break;
        }
    }

    private void getCode() {
        String phone = SPUtil.getString(WalletAddCardActivity.this, "mobile");
        if (TextUtils.isEmpty(phone)) {
            Intent intent = new Intent(WalletAddCardActivity.this, SettingBindTelActivity.class);
            startActivity(intent);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("mobile", phone + "");
            map.put("type", 6 + "");
            mProgressDialog.show();
            OkHttpUtil.getInstance().post(this, AppConst.URL_GET_CODE, map, new IResponse() {
                @Override
                public void onSuccess(JsonElement element) {
                    LogUtil.i(element.toString());
                    mProgressDialog.dismiss();
                    showToast("发送成功");
                    myCountDownTimer.start();
                }

                @Override
                public void onFailure(int statusCode, String errorMsg) {
                    LogUtil.i(errorMsg + "statusCode" + statusCode);
                    showToast(errorMsg);
                    mProgressDialog.dismiss();
                }
            });
        }
    }


    private void addCard() {
        String idNo = mEtIdNo.getText().toString();
        String cardName = mEtCardName.getText().toString();
        String cardNo = mEtCardNo.getText().toString();
        String idName = mEtIdName.getText().toString();
        String tel = mEtTelNo.getText().toString();
        String code = mEtCode.getText().toString();

        if (TextUtils.isEmpty(idName)) {
            showToast("名字不能为空");
            return;
        }
        if (TextUtils.isEmpty(cardNo)) {
            showToast("卡号不能为空");
            return;
        }
        if (!StringUtil.validateIdNo(idNo)) {
            showToast("身份证号码不正确");
            return;
        }

        if (!StringUtil.isMobile(tel)) {
            showToast("请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("idcard", idNo);
        map.put("phone", tel);
        map.put("bank_account", cardNo);
        map.put("true_name", idName);
        map.put("code", code);
        map.put("token", StringUtil.getToken(uid));
        map.put("bank_name", mBindCardInfo.getBank_name());
        map.put("bank_code", mBindCardInfo.getBank_code() + "");
        mProgressDialog.show();

        OkHttpUtil.getInstance().post(this, AppConst.URL_ADD_BANK, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                mProgressDialog.dismiss();
                showToast("添加成功");
                finish();


            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    private void showBankDialog() {

        OkHttpUtil.getInstance().post(this, AppConst.URL_BANK_LIST, null, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                mProgressDialog.dismiss();
                LogUtil.i(element.toString());
                Gson gson = new Gson();
                final ArrayList<BindCardInfo> bindCardInfos = gson.fromJson(element, new TypeToken<ArrayList<BindCardInfo>>() {
                }.getType());
                final String[] items = new String[bindCardInfos.size()];
                for (int i = 0; i < items.length; i++) {
                    items[i] = bindCardInfos.get(i).getBank_name();
                }

                AlertDialog.Builder listDialog = new AlertDialog.Builder(WalletAddCardActivity.this);
                listDialog.setTitle("选择银行");
                listDialog.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mBindCardInfo = bindCardInfos.get(which);
                        mEtCardName.setText(mBindCardInfo.getBank_name());
                        dialog.dismiss();
                    }
                });
                listDialog.show();

            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });


    }


    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            mTvGetCode.setClickable(false);
            mTvGetCode.setText(l / 1000 + "s");
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            mTvGetCode.setText("获取验证码");
            //设置可点击
            mTvGetCode.setClickable(true);
        }
    }

}
